/*
 * Created on 2005-6-11
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package leeon.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class DateIterator {

	public static void main(String[] args) throws Exception {
		DateIterator di = new DateIterator("2005-10-11", "2005-10-13");
		while (di.hasNext()) {
			System.out.println(di.get("yyyy-MM-dd'.jpg'"));
			System.out.println(di.get("'year='yy'&month='M'&day='dd"));
			di.next();
		}
	}	
	
	private Calendar start = null;
	private Calendar end = null;
	
	
	public DateIterator() throws Exception {
		init(new Date(), new Date());
		end.add(Calendar.DAY_OF_MONTH, 1);
	}
	
	public DateIterator(String start) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");		
		init(sdf.parse(start), new Date());
	}
	
	public DateIterator(String start, String end) throws Exception {
		this(start, end, "yyyy-MM-dd");
	}
	
	public DateIterator(String start, String end, String pattern) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);		
		init(sdf.parse(start), sdf.parse(end));
	}
	
	private void init(Date start, Date end) {
		this.start = Calendar.getInstance();
		this.start.setTime(start);
		this.end = Calendar.getInstance();
		this.end.setTime(end);
	}
	

	public void next() {
		start.add(Calendar.DAY_OF_MONTH, 1);
	}
	
	public boolean hasNext() {
		return start.before(end);
	}
	
	public String get() {
		return get("yyyy-MM-dd");
	}
	
	public String get(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(start.getTime());
	}
}
